﻿using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Admin.UserControls
{
    public partial class ucQuanLyNguoiDung : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        ToolBox tb = new ToolBox();
        BTbDangNhap dn = new BTbDangNhap();

        protected void Page_Load(object sender, EventArgs e)
        {

            LoadDangNhap();
            LoadDDL();
            LoadTKGVHV();

        }
        void LoadDangNhap()
        {
            gvDangNhap.AutoGenerateColumns = false;
            gvDangNhap.DataSource = BTbDangNhap.SelectAll();
            gvDangNhap.DataBind();
        }
        bool KiemTraDuLieu()
        {
            return BTbDangNhap.KiemTraDuLieu(new ETbDangNhap
            {
                TenDangNhap = txtTenDangNhap.Text
            });
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvDangNhap_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDangNhap.EditIndex = e.NewEditIndex;
            gvDangNhap.DataBind();
        }

        protected void gvDangNhap_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string maDN = gvDangNhap.DataKeys[e.RowIndex].Value.ToString();
                DataTable infodn = tb.GetDataTableById2("TbDangNhap_ByMaDN", maDN);
                if (infodn.Rows[0]["Quyen"].ToString() == "1")
                {
                    ThongBao("Bạn không được xóa tài khoản Administrator");
                }
                else
                {
                    BTbDangNhap.Delete(int.Parse(maDN));
                    BTbGiangVien.Delete(infodn.Rows[0]["TenDN"].ToString());
                    LoadDangNhap();
                    Session["action"] = 3;
                }
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến tài khoản này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvDangNhap_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDangNhap.EditIndex = -1;
            gvDangNhap.DataBind();
        }
        private void LoadDDL()
        {
            Methods.DonViGiangVien(ref ddldonvi, Session["TenDN"].ToString());
            Methods.ChucVu(ref ddlchucvu);
            Methods.Quyen(ref ddlquyen);

        }
        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            //if (!KiemTraDuLieu())
            //{


            if (tb.GetDataTableById2("TbGiangVien_GetMaGV", txtTenDangNhap.Text).Rows.Count > 0)
            {
                ThongBao("Tên đăng nhập đã tồn tại..!");
            }
            else
            {
                if (ddldonvi.SelectedValue.ToString() == "-1" || ddlchucvu.SelectedValue.ToString() == "-1")
                {
                    ThongBao("Bạn chưa chọn đơn vị hoặc chức vụ");
                    btThemMoi.Visible = true;
                    btSua.Visible = false;
                }
                else
                {
                    if (ddlquyen.SelectedValue.ToString() != "")
                    {
                        ETbDangNhap dn1 = new ETbDangNhap();
                        dn1.TenDangNhap = txtTenDangNhap.Text;
                        dn1.KhoaTaiKhoan = Convert.ToInt32(ddlKhoaTK.SelectedValue.ToString());
                        dn1.MatKhau = dn.GetMD5(txtMatKhau.Text + dn.Radomchar());
                        dn1.Quyen = ddlquyen.SelectedValue.ToString();
                        dn1.RadomPass = dn.Radomchar();
                        BTbDangNhap.InsertDangNhap(dn1);
                        ETbGiangVien tttk = new ETbGiangVien();
                        tttk.MaGV = txtTenDangNhap.Text;
                        tttk.TenGV = txthoten.Text;
                        tttk.Sdt = txtsdt.Text;
                        tttk.MaDonVi = Convert.ToInt32(ddldonvi.SelectedValue.ToString());
                        tttk.MaChucVu = Convert.ToInt32(ddlchucvu.SelectedValue.ToString());
                        tttk.Anh = "";
                        BTbDangNhap.InsertToTbGiangVien(tttk);
                        //ThongBao("Thêm mới thành công");
                        LoadDangNhap();
                    }
                    else
                    {
                        ThongBao("Bạn chưa chọn quyền của tài khoản..!");
                    }
                    Huy();
                    btThemMoi.Visible = true;
                    btSua.Visible = false;
                    LoadDDL();
                    Session["action"] = 1;
                }
            }

            //}
            //else
            //{
            //    ThongBao("Tài khoản này đã tồn tại!");

            //}
        }
        private void Huy()
        {
            txtTenDangNhap.Text = "";
            txthoten.Text = "";
            txtMatKhau.Text = "";
            txtsdt.Text = "";
            txtXacNhanMK.Text = "";
            HuyCheckbox();
        }
        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }
        protected void gvDangNhap_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDangNhap.PageIndex = e.NewPageIndex;
            gvDangNhap.DataBind();
        }
        public static String maDN;
        public static String TenDN;
        protected void gvDangNhap_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                pnthemtk.Visible = true;
                btSua.Visible = true;
                btThemMoi.Visible = false;
                int index = Convert.ToInt32(e.CommandArgument);
                maDN = gvDangNhap.DataKeys[index].Value.ToString();
                TenDN = gvDangNhap.Rows[index].Cells[0].Text.Trim();
                LoadDataSua();
                txtTieuDe.InnerText = "Sửa khóa học";
                DataTable infodn = tb.GetDataTableById2("TbDangNhap_ByMaDN", maDN);
                txtTenDangNhap.Text = infodn.Rows[0]["TenDN"].ToString();
                txthoten.Text = infodn.Rows[0]["TenGV"].ToString();
                txtsdt.Text = infodn.Rows[0]["Sdt"].ToString().Trim();
                ddlquyen.SelectedValue = infodn.Rows[0]["MaQuyen"].ToString();
                ddlKhoaTK.SelectedValue = infodn.Rows[0]["KhoaTaiKhoan"].ToString();
                ddlchucvu.SelectedValue = infodn.Rows[0]["Machucvu"].ToString();
                ddldonvi.SelectedValue = infodn.Rows[0]["MaDonvi"].ToString();
                DataTable cnquyen = tb.GetDataTableById("TbDangNhap_GetCNQuyenByMaQuyen", infodn.Rows[0]["Quyen"].ToString());
                HuyCheckbox();
                for (int i = 0; i < cnquyen.Rows.Count; i++)
                {
                    for (int j = 1; j < 57; j++)
                    {
                        CheckBox a = FindControl("ql" + j.ToString()) as CheckBox;
                        if (cnquyen.Rows[i]["MaCN"].ToString() == j.ToString())
                        {
                            a.Checked = true;
                        }
                    }
                }
            }
        }
        private void LoadDataSua()
        {
            mk.Visible = false;
            xnmk.Visible = false;
            hoten.Visible = true;
            txtTenDangNhap.Enabled = false;
            txtsdt.Enabled = false;
            txthoten.Enabled = false;
            tbchucnang.Enabled = false;
        }
        private void LoadDataThem()
        {
            mk.Visible = true;
            xnmk.Visible = true;
            hoten.Visible = true;
            txtTenDangNhap.Enabled = true;
            txtsdt.Enabled = true;
            txthoten.Enabled = true;
            tbchucnang.Enabled = false;
        }
        protected void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!KiemTraDuLieu())
                //{
                BTbDangNhap.Update_PhanQuyen(new ETbDangNhap
                {
                    MaDN = int.Parse(maDN),
                    Quyen = ddlquyen.SelectedValue.ToString(),
                    KhoaTaiKhoan = int.Parse(ddlKhoaTK.SelectedValue.ToString())
                });
                BTbDangNhap.Update_ChucVuDonVi(new ETbGiangVien
                {
                    MaGV = TenDN,
                    MaChucVu = int.Parse(ddlchucvu.SelectedValue.ToString()),
                    MaDonVi = int.Parse(ddldonvi.SelectedValue.ToString())
                });
                Session["action"] = 2;
                LoadDangNhap();
                //}
            }
            catch { }
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {

            gvDangNhap.DataSource = BTbDangNhap.SearchDangNhap(new ETbDangNhap
            {
                TenDangNhap = txtTimKiem.Text
            });

            gvDangNhap.DataBind();

        }

        protected void btImport_Click(object sender, EventArgs e)
        {

        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        protected void btThem_ServerClick(object sender, EventArgs e)
        {
            btThemMoi.Visible = true;
            pnthemtk.Visible = true;
            Huy();
            LoadDDL();
            LoadDataThem();
            btSua.Visible = false;
        }
        private void HuyCheckbox()
        {
            for (int j = 1; j < 57; j++)
            {
                CheckBox c = FindControl("ql" + j.ToString()) as CheckBox;
                c.Checked = false;
            }
        }
        protected void ddlquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlquyen.SelectedValue.ToString() == "-1")
            {
                ThongBao("Bạn phải chọn quyền.");
            }
            else
            {
                DataTable dt = tb.GetDataTableById("TbChucNang_Quyen_GetByMaQuyen", ddlquyen.SelectedValue.ToString());
                HuyCheckbox();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 1; j < 57; j++)
                    {
                        CheckBox a = FindControl("ql" + j.ToString()) as CheckBox;
                        if (dt.Rows[i]["MaCN"].ToString() == j.ToString())
                        {
                            a.Checked = true;
                        }
                    }
                }
                //tbchucnang.Enabled = false;
            }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            pnthemtk.Visible = false;
        }


        protected void GvTaiKhoanHVGV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GvTaiKhoanHVGV_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void GvTaiKhoanHVGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
        private void LoadTKGVHV()
        {
            gvDSTKGVHV.AutoGenerateColumns = false;
            gvDSTKGVHV.DataSource = BTbDangNhap.SelectbyGVHV();
            gvDSTKGVHV.DataBind();

        }
        protected void gvDSTKGVHV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string maDN = gvDSTKGVHV.DataKeys[e.RowIndex].Value.ToString();
                DataTable infodn = tb.GetDataTableById2("TbDangNhap_ByMaDN", maDN);
                BTbDangNhap.Delete(int.Parse(maDN));
                LoadTKGVHV();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến tài khoản này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvDSTKGVHV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDSTKGVHV.PageIndex = e.NewPageIndex;
            gvDSTKGVHV.DataBind();
        }

        protected void gvDSTKGVHV_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void imgbtmokhoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton btna = sender as ImageButton;
                GridViewRow row = (GridViewRow)btna.NamingContainer;
                string id = gvDSTKGVHV.DataKeys[row.RowIndex].Value.ToString();
                BTbDangNhap.UpdateTrangThaiTK(id, "0");
                LoadTKGVHV();
                Session["action"] = 2;
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void imgbtkhoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton btna = sender as ImageButton;
                GridViewRow row = (GridViewRow)btna.NamingContainer;
                string id = gvDSTKGVHV.DataKeys[row.RowIndex].Value.ToString();
                BTbDangNhap.UpdateTrangThaiTK(id, "1");
                LoadTKGVHV();
                Session["action"] = 2;
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void gvDSTKGVHV_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton huy = (ImageButton)e.Row.FindControl("imgbtkhoa");
                ImageButton Accept = (ImageButton)e.Row.FindControl("imgbtmokhoa");
                Label lblstatusid = (Label)e.Row.FindControl("lblstatusblock");
                if (lblstatusid.Text == "1")
                {
                    huy.Width = 20;
                    huy.Height = 20;
                    huy.BorderWidth = 1;
                    huy.BorderColor = System.Drawing.Color.Gray;
                }
                else if (lblstatusid.Text == "0")
                {
                    Accept.Height = 20;
                    Accept.Width = 20;
                    Accept.BorderWidth = 1;
                    Accept.BorderColor = System.Drawing.Color.Green;
                }
            }
        }
    }
}